<%@page import="org.proddevplm.dao.data.activities.Activity"%>
<%@page import="org.proddevplm.dao.data.stakeholders.Stakeholder"%>
<%@page import="java.util.List"%>
<%@page import="org.proddevplm.constants.ControllerConstants"%>
<%@page import="org.proddevplm.business.activity.constants.ActivityConstants" %>
<%@page import="org.proddevplm.business.tools.constants.ToolsConstants" %>

<script type="text/javascript">

	$(function(){
		$( "button" ).button();
		$("#shButtonset").buttonset();
		$("#shButtonset").hide();
		$("#approveShDB").tablesorter({
			widgets: ['zebra'],
			headers:{4: {sorter: false}}
		});
	});
	
	function loadSHPage(page){
		$("#shSelectPage").load(page);
	}
	
	function loadSHPageWithReq(request, page) {
        $.post(request, function(data) {
            $("#shSelectPage").load(page);
        });
	}
	
	function newSH(){
		loadSHPage("content/project/devTools/workspace/stakeholders/newSH.jsp");
	}
	
	function viewGuidelines(){
		$("#guidelines").show();
	}
	
	function viewDatabase(){
		loadSHPageWithReq("/proddev/controller?manager=dataAccessManager&method=4", "content/project/devTools/workspace/stakeholders/viewDB.jsp");
	}
	
	function removeSH(shName, tRow){
		var confirmSHDeletion = confirm("Do you want to remove this stakeholder?");
		if(confirmSHDeletion){
			var arguments = "shName="+shName;
			var tableRow = tRow;
			$("#"+tableRow).hide();
			ajaxCall("toolsManager", "6", arguments, null);
			alert('Stakeholder deleted successfully');
		}
	}
	
	function approveActivity(name){
		$("#activityName").val(name);
		document.forms['approveForm'].submit();
	}
	
	function rejectActivity(name){
		$("#activityName").val(name);
		$("#rejectDialog").dialog("open");
	}
	
	$( "#rejectDialog" ).dialog({
		autoOpen: false,
		height: 350,
		width: 350,
		modal: true,
		buttons: {
			"Propose change": function() {
				var name = document.getElementById("activityName").value;
				var arguments = "activityName="+name+"&"+buildParameter("changeSubject")+"&"+buildParameter("changeDetails");
				ajaxCall("toolsManager", "21", arguments, forward);
				$( this ).dialog( "close" );
				},
			Cancel: function() {
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			$( this ).dialog( "close" );
		}
	});
	
	function forward(data){
		document.forms["rejectForm"].submit();
	}
	
	function addNewSH(activityName){
		$( "#dialog-newSH" ).dialog("open");
		$("#activityName").val(activityName);
	}
	
	$( "#dialog-newSH" ).dialog({
		autoOpen: false,
		height: 450,
		width: 600,
		modal: true,
		buttons: {
			"Add new stakeholder": function() {
				var activityName = document.getElementById('activityName').value;
				addExtraSH(activityName);
				$( this ).dialog( "close" );
				},
			Cancel: function() {
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			$( this ).dialog( "close" );
		}
	});

</script>

<%
List<Stakeholder> shByActName = (List)session.getAttribute("shByActName");
List<Activity> activitiesForUser = (List)session.getAttribute("activitiesForUser");
List<Stakeholder> shResults = (List)session.getAttribute("shResults");
String[] lcStages = {"Concept", "Development", "Production", "Use", "Support", "Retirement"};
String activityName = "";
%>

<div class="pageTitle">
	<div style="text-align:left;">
		<a href="/proddev/controller?manager=dataAccessManager&method=21">
			<img src="/proddev/images/arrowleft.png" align="middle" height="20" width="30" style="border:0px"></a>
	</div>
	<p style="font-size:small; font-weight:bold; margin-top:-20px">Stakeholders' Identification</p>

</div>

<%if (shByActName==null){ %>

	<div style="margin:10 0 0;">
		<div style="float:left; text-align:left; font-weight:bold">
			Please choose the activity you want to complete
		</div>
		<select class="regInput" id="activitySelect" name="activitySelect" onchange="$('#shButtonset').show()" size="1" style="width:25%; float:left; margin-left:10px">
			<option SELECTED>-select activity-</option>
			<%if(activitiesForUser!=null){
				for(int i=0; i<activitiesForUser.size(); i++) {
					Activity activity = (Activity)activitiesForUser.get(i);%>
					<option value="<%=activity.getName()%>"><%=activity.getName() %></option>
				<%}
			}
			%>		
		</select>
		<div style="clear:both"></div>
	</div>
	
	<div id="shButtonset" style="width:auto; margin-top:10px; text-align:left;">
		<input type="radio" name="radio" id="newSH" onclick="newSH()" /><label for="newSH"><b>New Stakeholder</b></label>
		<input type="radio" name="radio" id="viewGuidelines" onclick="viewGuidelines()"/><label for="viewGuidelines"><b>View Guidelines</b></label>
		<input type="radio" name="radio" id="viewDatabase" onclick="viewDatabase()" /><label for="viewDatabase"><b>View existing DB</b></label>
	</div>
	
	
	<div style="width:auto;">
		
		<jsp:include page="/pages/content/project/devTools/workspace/stakeholders/shSelectionPage.jsp"></jsp:include>
		
	</div>
<%}else{ 
	Stakeholder sh = shResults.get(0);
	activityName = sh.getActivityName();%>
	<div align="center">
		<div style="width:100%">
			<div style="margin-top:10px; text-align:left; ">
				<button id="addNewSH" name="addNewSH" onclick="addNewSH('<%=activityName%>')" style="width:20%; font-weight: bold;">Add new stakeholder</button>
			</div>
			<table id="approveShDB" class="tablesorter" style="width:100%" rules="rows" frame="hsides" bordercolor="#DBDBDB">
				<thead>
					<tr>
						<th style="width:20%">Stakeholder's name</th>
						<th style="width:35%">Stakeholder's details</th>
						<th style="width:20%">Life cycle stage</th>
						<th style="width:20%">Added by</th>
						<th style="width:5%">Action</th>	
					</tr>		
				</thead>
				<tbody>
					<%
					for(int i=0; i<shResults.size(); i++){
						Stakeholder stakeholder = shResults.get(i);
						activityName = stakeholder.getActivityName();%>
						<tr id="row<%=i+1%>">
							<td><%=stakeholder.getShName()%></td>
							<td><%=stakeholder.getDetails()%></td>
							<td><%
							String stages = stakeholder.getLcStage();
							int[] indexes = new int[6];
							for (int j=0; j<stages.length(); j++)
								indexes[j] = Integer.valueOf(stages.substring(j,j+1));
							for (int k=0; k<stages.length(); k++){
								int aux = indexes[k]-1;%>
								<%= lcStages[aux]+"</br>"%>
							<%}
							%></td>
							<td><%=stakeholder.getAuthor() %></td>
							<td style="text-align:center"><img src="/proddev/images/delete1.png" onclick="removeSH('<%=stakeholder.getShName()%>', 'row<%=i+1%>')" align="middle" width="20" height="20"/></td>
						</tr>
					<%}
					%>
				
				</tbody>
			</table>
		</div>
		
		<div id="dialog-newSH" title="Add new stakeholder" style="font-family:verdana; font-size:small; display:none">
			<p>Please fill in the information for stakeholder</p>
	
			<jsp:include page="addExtraSH.jsp"></jsp:include>
		</div>
		
		<div style="margin-top:10px; width:100%">
			<button id="rejectActivity" name="rejectActivity" onclick="rejectActivity('<%=activityName%>')" style="width:150px; font-weight: bold; float:left">Reject activity</button>
			<button id="approveActivity" name="approveActivity" onclick="approveActivity('<%=activityName%>')" style="width:150px; font-weight: bold; float:right">Approve activity</button>
			<div style="clear:both"></div>
		</div>
		
		<div id="rejectDialog" title="Propose changes" style="display:none">
			<jsp:include page="/pages/content/project/devTools/workspace/changes/changeReport.jsp"></jsp:include>
		</div>
	
		<form name="rejectForm" method="post" action="/proddev/controller" style="margin:0px">
			<input type = "hidden" name = "<%=ControllerConstants.MANAGER_KEY%>" value = "<%=ControllerConstants.TOOLS_MANAGER%>"/>
			<input type = "hidden" name = "<%=ControllerConstants.METHOD_KEY%>" value = "<%=ToolsConstants.STAKEHOLDERS_FWD%>"/>
		</form>
		
		<form name="approveForm" method="post" action="/proddev/controller" style="margin:0px">
			<input type = "hidden" name = "<%=ControllerConstants.MANAGER_KEY%>" value = "<%=ControllerConstants.ACTIVITY_MANAGER%>"/>
			<input type = "hidden" name = "<%=ControllerConstants.METHOD_KEY%>" value = "<%=ActivityConstants.UPDATE_ACTIVITY%>"/>
			<input type="hidden" name="activityName" id="activityName">
		</form>
	</div>
<%}%>